package it.univaq.tlp.library.presentation;

import it.univaq.tlp.library.TitleManager;
import it.univaq.tlp.library.business.impl.JDBCTitleManager;
//import it.univaq.tlp.library.business.impl.MemoryTitleManager;
import it.univaq.tlp.library.business.model.Title;
import it.univaq.tlp.library.business.model.TitleKind;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.util.Iterator;
import java.util.List;

public class Library {
	
	public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	
	public static void main(String[] args) throws IOException{
		
		try {
			Class.forName("com.mysql.jdbc.Driver" );
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		TitleManager manager = new JDBCTitleManager();
		
		while (true) {
			System.out.println("(1) Visualizza titoli");
			System.out.println("(2) Inserisci il titolo");
			System.out.println("(3) Modifica il titolo");
			System.out.println("(4) Elimina il titolo");
			System.out.println("(5) Esci");
			String line = reader.readLine();
			if ("1".equals(line)) {
				List<Title> titles = manager.findAllTitles();
				viewTitles(titles);

			} else if ("2".equals(line)) {
				Title title = createTitle();
				
				manager.createTitle(title);

			} else if ("3".equals(line)) {

			} else if ("4".equals(line)) {

			} else if ("5".equals(line)) {

			}
		}
		
		
	}
	
	private static void viewTitles(List<Title> titles) {
		// TODO Auto-generated method stub
		/*
		Iterator<Title> i = titles.iterator();
		while(i.hasNext()){
			Title title = i.next();
		}
		
		for(Iterator<Title> i = titles.iterator();i.hasNext();){
			Title title = i.next();
		}
		*/		
		for(Title title : titles){ //for(Type t: elements) {}
			System.out.println("Titolo: " + title.getName() + 
					", Autore: " + title.getAuthor() + 
					", Descrizione: " + title.getDescription() +   
					", ISBN: " + title.getIsbn() +
					", Anno di pubblicazione: " + title.getPublicationYear() +
					", Editore: " + title.getEditor() +
					", Tipo: " + title.getTitleKind().getName() 
		);
		}
		
		/* implementation with array
		for (int i=0; i < titles.length; i++){
			Title title = titles[i];
			System.out.println("Titolo: " + title.getName() + 
								", Autore: " + title.getAuthor() + 
								", Descrizione: " + title.getDescription() +   
								", ISBN: " + title.getIsbn() +
								", Anno di pubblicazione: " + title.getPublicationYear() +
								", Editore: " + title.getEditor() +
								", Tipo: " + title.getTitleKind().getName() 
					);
			
		}
		*/
	}

	public static Title createTitle() throws IOException {
		
		System.out.println("Inserisci il nome");
		String name = reader.readLine();
		System.out.println("Inserisci l'autore");
		String author = reader.readLine();
		System.out.println("Inserisci la descrizione");
		String description = reader.readLine();
		System.out.println("Inserisci l'isbn");
		String isbn = reader.readLine();
		System.out.println("Inserisci l'anno di pubblicazione");
		int publicationYear = Integer.parseInt(reader.readLine());
		System.out.println("Inserisci l'editore");
		String editor = reader.readLine();
		
		System.out.println("Inserisci il tipo ( 1: libro, 2: rivista, 3: periodico, 4. quotidiano)");
		int titleKindId= Integer.parseInt(reader.readLine());
		TitleKind titleKind = new TitleKind(titleKindId, "libro" ); 
		//nome, autore, descrizione, ISBN, anno, editore, tipo
		
		Title title = new Title(name, author, description, isbn, publicationYear, editor, titleKind);	
		
		return title;
	}
	
}
